import json, base64
from game.maze import MAZE
from support.sys.shell import Shell
from support.sys.executor import Executor
class Dispatch:
    def doCmd(request):
        msg = ["not support"]
        type = 0
        cmd = request.split()
        if(cmd[0] == MAZE.CMD):
            params = MAZE.parseParams(cmd[1:])
            msg = MAZE.generate(params.model, params.row, params.column)
            type = 2
        return {"type":type, "msg":msg}

    def onRequest(cmd):
        response = []
        request = json.loads(cmd)
        cmd = str(base64.b64decode(request["BODY"]), "utf-8")
        if(request["CMD"] == 0):
            response.append(Dispatch.doCmd(cmd))
        else:
            response.append(Executor.run(cmd, globals()))
        return json.dumps(response)
